﻿@model DHQGHN.PM0.Models.Admin.UserInGroupModel
@{
    var lstUser = ViewBag.lstUser;
    int columns = 2;
    var rowsMenu = lstUser.Items.Count % columns == 0 ? lstUser.Items.Count / columns :
                    (lstUser.Items.Count + (columns - (lstUser.Items.Count % columns))) / columns;
    int index = 0;
}

@*@using (
                                Html.BeginForm(
                                    actionName: "actionCreateOrUpdateUserInGroup", controllerName: "UserInGroup", routeValues: new { }, method: FormMethod.Post, htmlAttributes: new { @id = "user-in-group-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
                                    )
                                )
    {*@
<div class="modal-body min-width">
    <div class="form-group">
        @Html.AntiForgeryToken()
        @Html.HiddenFor(m => m.UserInGroupId)
        <div>
            @Html.Label("Tên ứng dụng", new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
            @Html.DropDownList("appid", new SelectList(ViewBag.lstApp, "value", "text", 0), new { @id = "input-appid", @name = "appid", @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;", @disabled = "disabled" })
        </div>
        <div>
            @Html.LabelFor(m => m.GroupId, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
            @Html.DropDownListFor(m => m.GroupId, new SelectList(ViewBag.lstGroup, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;", @disabled = "disabled" })
            @Html.ValidationMessageFor(m => m.GroupId, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
        </div>

        <div>
            <div>
                @Html.Label("Người dùng", new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                &nbsp;<input type="text" class="txtsearchname TextBoxContent" style="width: 260px !important" id="input-username" name="username" placeholder="Nhập tên">
            </div>
            <div style="text-align: center;">
                <button class=" btn btn-primary" type="button" onclick="return btnSearchClick();">
                    <span class="glyphicon glyphicon-search" style="margin-left:5px"></span>Tìm kiếm
                </button>
            </div>
            @*@Html.LabelFor(m => m.Username, new { @class = "labels", style = "width:100px;" })<span> &nbsp; </span>*@
            <div style="max-height: 200px; overflow: auto; resize: none;" id="List">
                @if (!(lstUser.Items.Count == 0 || lstUser == null))
                {
                    <table class="tbl-list" style="margin: 10px auto;width:500px;">

                        @for (int i = 0; i < rowsMenu; i++)
                        {
                            <tr>
                                @for (int j = 0; j < columns; j++)
                                {
                                    index = columns * i + j;
                                    if (index >= lstUser.Items.Count) { break; }
                                    <td style="width:50%" class="td-onclick">
                                        <div id="checkboxUserChecked">
                                            <input type="checkbox" name="@lstUser.Items[index].Value" />&nbsp;&nbsp;&nbsp;@lstUser.Items[index].Text
                                        </div>
                                    </td>
                                }
                                @if (i == rowsMenu - 1)
                                {
                                    if (lstUser.Items.Count % columns != 0)
                                    {
                                        int cellCount = columns - (lstUser.Items.Count % columns);
                                        for(int k = 0 ; k< cellCount;k++)
                                        {
                                            <td></td>
                                        }
                                    }
                                }
                            </tr>
                        }
                    </table>
                }
                else
                {
                    <span class="field-validation-error" style="margin-left:112px">Nhóm đã gán hết người dùng</span>
                }

            </div>
            <br />
        </div>
    </div>
</div>
@*}*@

<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")

<script>
    @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>
    //var $userInGroupForm = $("#user-in-group-form");
    function save() {
        var result = false;
        var listID = getListUserID();
        if ($('#GroupId').val().trim() > 0) {
            $("#GroupId").prop("disabled", false);
            $.ajax({
                contentType: "application/json",
                type: "POST",
                url: '@Url.Action("actionCreateOrUpdateUserInGroup", "UserInGroup")',
                //url: $userInGroupForm.attr('action'),
                data: JSON.stringify({ GroupId: $('#GroupId').val().trim(), listID: listID }),
                dataType: 'json',//chú ý là form đặc biệt sthì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                        $("#GroupId").prop("disabled", true);
                    }
                }
            });
        }
        return result;

    }

    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });

    function getListUserID() {
        var values = [];
        $('#checkboxUserChecked input:checked').each(function () {
            values.push(this.name);
        });
        return values;
    }

    //$('#select_all').click(function (event) {
    //    if (this.checked) {
    //        // Iterate each checkbox
    //        $(':checkbox').each(function () {
    //            this.checked = true;
    //        });
    //    }
    //    else {
    //        $(':checkbox').each(function () {
    //            this.checked = false;
    //        });
    //    }
    //});

    function btnSearchClick() {
        OnCriterianame(0);
    }

    var crrPageIndex;
    function OnCriterianame(pageIndex) {
        var username = $('#input-username').val().trim();
        crrPageIndex = pageIndex;
        //username = username != null ? username.val().trim() : "";
        $.ajax({
            url: '@Url.Action("actionSearchname", "UserInGroup")',
            dataType: "html",
            type: "POST",
            headers: {
                'VerificationToken': '@GetAntiForgeryToken()'
            },
            contentType: "application/json",
            data: JSON.stringify({ SeatchParam: username, groupID: $('#GroupId').val().trim(), AppId: $('#input-appid').val() }),
            success: function (response) {
                var $catlist = $('#List');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                    $catlist.html(response);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    $('input.txtsearchname').bind('keypress', function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == 13 && OnCriterianame) {
            OnCriterianame(0);
        }
    });

</script>
